//
//  HomeViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/1/4.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "HomeViewController.h"
#import "LgViewController.h"
#import "MsgCenterViewController.h"
#import "MarketHeaderCell.h"
#import "PublishPostViewController.h"

@interface HomeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UILabel *noticecount;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"首页"];
    
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 20, 20);
    [buttonNotice setBackgroundImage:[UIImage imageNamed:@"topnotice.png"] forState:UIControlStateNormal];
    self.noticecount = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 10, 10)];
    self.noticecount.font = [FontTool fontWithSize:10];
    self.noticecount.textAlignment = NSTextAlignmentCenter;
    self.noticecount.textColor = [UIColor whiteColor];
    self.noticecount.backgroundColor = [UIColor colorWithHexString:@"0EA1B6"];
    self.noticecount.layer.masksToBounds = YES;
    self.noticecount.layer.cornerRadius = 5.f;
    self.noticecount.text = @"1";
    self.noticecount.userInteractionEnabled = NO;
    [buttonNotice addSubview:self.noticecount];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(clickNotice:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    
    if (![UserData isLogin]) {
        LgViewController *loginViewController = [[LgViewController alloc]initWithNibName:@"LgViewController" bundle:nil];
    //    [self presentViewController:loginViewController animated:NO completion:^(){}];
        [self.navigationController pushViewController:loginViewController animated:NO];
    }
    [self getMsgCount];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickNotice:(id)sender {
    MsgCenterViewController *vc = [[MsgCenterViewController alloc] initWithNibName:@"MsgCenterViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getMsgCount {
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = [NSString stringWithFormat:@"%@/sys/index/message/count", APIYIGOU];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSInteger count = [[dict objectForKey:@"data"] integerValue];
        self.noticecount.text = [NSString stringWithFormat:@"%ld", count];
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 188;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MarketHeaderCell *cell = GET_CELL(MarketHeaderCell, @"MarketHeaderCell");
    [cell fillData];
    return cell;
}

@end
